//
//  HomeViewController.m
//  xiaoyiNew
//
//  Created by 讯一 on 16/12/25.
//  Copyright © 2016年 讯一. All rights reserved.
//

#import "HomeViewController.h"

#import "UIButton+Various.h"

#import "MJExtension.h"
#import "XMNetworking.h"
#import "shoppingmodel.h"

//三个cell类
#import "TSHomeMenuCell.h"
#import "TSShopImageCell.h"
#import "threeTableViewCell.h"
//headview
#import "headView.h"
#import "secondtoforhead.h"
//
#import "ZXShopCartViewController.h"


@interface HomeViewController () <UITableViewDelegate,UITableViewDataSource,TermCellDelegate,TSHomeMenuCellDelegate>
//左上角button
@property (nonatomic,strong) UIButton *leftButton;
//定位名字
@property (nonatomic,copy)NSString *locationName;
//tableview
@property (nonatomic,strong)UITableView *maintable;

@property (nonatomic, strong) NSMutableArray *menuArray;

//网络数据数组
@property (nonatomic , strong)NSMutableArray *allArray;
@property (nonatomic ,strong) NSMutableArray *daysArray;

@property (nonatomic, strong)NSMutableArray *timeArray;

@property (nonatomic ,strong)NSMutableArray *brandArray;


@end

@implementation HomeViewController

static NSString *customCell1 = @"CustomCell1";
static NSString *customCell2 = @"CustomCell2";
static NSString *customCell3 = @"CustomCell3";

- (NSMutableArray *)allArray
{
    if (!_allArray) {
        _allArray = [NSMutableArray array];
    }
    return _allArray;
}
- (void)viewWillAppear:(BOOL)animated{
    self.navigationController.tabBarController.tabBar.hidden = NO;
}
- (void)loadView{
    [super loadView];
    [self networking];

}
- (void)viewDidLoad {
    [super viewDidLoad];
    
   self.navigationItem.title = @"小易学院";
    _daysArray = [[NSMutableArray alloc]init];
    _timeArray = [[NSMutableArray alloc]init];
    _brandArray = [[NSMutableArray alloc]init];
    
   // UIButton *button3 = [UIButton initButtonEdgeInsetsStyle:HSButtonEdgeInsetsStyleLeft buttonFrame:CGRectMake(0, 0, 200, 200) titleImage:@"locationicon" titleLabel:@"重庆大学" titleColor:[UIColor redColor] font:[UIFont systemFontOfSize:20] imageAndTitleSpace:20];
    [self inizdata];
    [self.view addSubview:self.maintable];
    
    
//    self.navigationItem.rightBarButtonItem = [UINavigationItem alloc]
    //self.navigationItem.leftBarButtonItem = button3;
    
}
#pragma mark -- 网络请求
//网络请求
- (void)networking{
    [XMCenter setupConfig:^(XMConfig * _Nonnull config) {
        config.callbackQueue = dispatch_get_main_queue();
    }];
    [XMCenter sendRequest:^(XMRequest * _Nonnull request) {
        request.url = @"http://120.25.210.173:8080/TextServer/GetXiaoyiXinCommditys";
        request.responseSerializerType = kXMResponseSerializerRAW;
        request.httpMethod = kXMHTTPMethodPOST;
    } onSuccess:^(id  _Nullable responseObject) {
        NSArray *shopping = [shoppingmodel mj_objectArrayWithKeyValuesArray:responseObject];
        self.allArray = [NSMutableArray arrayWithArray:shopping];
        for (int i = 0; i < _allArray.count; i ++) {
            shoppingmodel *model1 = shopping[i];
            if ([model1.fenleiid isEqualToString:@"3"]) {
                [_daysArray addObject:model1];
            }else if ([model1.fenleiid isEqualToString:@"4"]){
                [_timeArray addObject:model1];
            }else if ([model1.fenleiid isEqualToString:@"5"]){
                [_brandArray addObject:model1];
            }
        }
        NSIndexSet *indexSet=[[NSIndexSet alloc]initWithIndex:1];
        [self.maintable reloadSections:indexSet withRowAnimation:nil];
    } onFailure:^(NSError * _Nullable error) {
        NSLog(@"%@", [error localizedDescription]);
    }];

}

#pragma mark -- 布局plist

- (void)inizdata{
    NSString *plistPath = [[NSBundle mainBundle]pathForResource:@"menuData" ofType:@"plist"];
    self.menuArray = [[NSMutableArray alloc]initWithContentsOfFile:plistPath];
}

#pragma mark -- tableview代理方法

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return 1;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 4;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return 180;
}
#pragma mark--headerfooter
- (CGFloat)tableView:(UITableView* )tableView heightForFooterInSection:(NSInteger)section
{
    return 10;
}
//section底部视图
-(UIView *)tableView:(UITableView* )tableView viewForFooterInSection:(NSInteger)section
{
    UIView *view=[[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 10)];
    view.backgroundColor = [UIColor clearColor];
    return view;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    if (section == 0) {
        return 180;
    }
    return 40;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    if (section == 0) {
        headView *view = [[headView alloc]initWithFrame:CGRectMake(0, 0, Screen_Width, 180)];
        return view;
    }else if (section == 1){
        secondtoforhead *head1 = [[secondtoforhead alloc]initWithFrame:CGRectMake(0, 0, Screen_Width, 40) img:[UIImage imageNamed:@"hot"] title:@"每日推荐"];
        return head1;
    }else if (section == 2){
        secondtoforhead *head2 = [[secondtoforhead alloc]initWithFrame:CGRectMake(0, 0, Screen_Width, 40) img:[UIImage imageNamed:@"xianshiqianggouicon"] title:@"限时抢购"];
        return head2;
    }
    secondtoforhead *head3 = [[secondtoforhead alloc]initWithFrame:CGRectMake(0, 0, Screen_Width, 40) img:[UIImage imageNamed:@"pinpaizhuanquicon"] title:@"品牌专区"];
    return head3;
}

- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 0) {
        TSHomeMenuCell *firstCell = [TSHomeMenuCell cellWithTableView:tableView menuArray:self.menuArray];
        firstCell.selectionStyle =UITableViewCellSelectionStyleNone;
        firstCell.delegate = self;
        return firstCell;
    }else if (indexPath.section == 1){
        TSShopImageCell *sectionCell = [TSShopImageCell cellWithTableView:tableView menuArray:self.daysArray];
        //TSShopImageCell *sectionCell = [TSShopImageCell cellWithTableView:tableView array:self.menuArray];
        sectionCell.selectionStyle =UITableViewCellSelectionStyleNone;
        sectionCell.delegate = self;
        return sectionCell;
    }else if (indexPath.section == 2){
    TSShopImageCell *sectionCell = [TSShopImageCell cellWithTableView:tableView menuArray:self.timeArray];
    //TSShopImageCell *sectionCell = [TSShopImageCell cellWithTableView:tableView array:self.menuArray];
    sectionCell.selectionStyle =UITableViewCellSelectionStyleNone;
    sectionCell.delegate = self;
        return sectionCell;
    }else if(indexPath.section == 3){
        TSShopImageCell *sectionCell = [TSShopImageCell cellWithTableView:tableView menuArray:self.brandArray];
        //TSShopImageCell *sectionCell = [TSShopImageCell cellWithTableView:tableView array:self.menuArray];
        sectionCell.selectionStyle =UITableViewCellSelectionStyleNone;
        sectionCell.delegate = self;
        return sectionCell;
    }
    TSShopImageCell *sectionCell = [TSShopImageCell cellWithTableView:tableView menuArray:self.brandArray];
    //TSShopImageCell *sectionCell = [TSShopImageCell cellWithTableView:tableView array:self.menuArray];
    sectionCell.selectionStyle =UITableViewCellSelectionStyleNone;
    sectionCell.delegate = self;
    return sectionCell;

}
//点击事件
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    
}
#pragma mark--action
- (void)choseTerm:(UITapGestureRecognizer *)button{
    NSLog(@"第几个商品%ld",button.view.tag);
    
}
- (void)push:(UITapGestureRecognizer *)sender{
    if (sender.view.tag == 1004) {
        
        ZXShopCartViewController *nav = [[ZXShopCartViewController alloc]init];
        nav.allgoodsarr = self.allArray;
        
        [self.navigationController pushViewController:nav animated:YES];
    }
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/
#pragma mark--getter/setter
- (UITableView *)maintable{
    if (_maintable == nil) {
        _maintable = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, Screen_Width, Screen_Height) style:UITableViewStyleGrouped];
        _maintable.delegate = self;
        _maintable.dataSource = self;
        
        [_maintable registerClass:[TSHomeMenuCell class] forCellReuseIdentifier:customCell1];
        [_maintable registerClass:[TSShopImageCell class] forCellReuseIdentifier:customCell2];
        [_maintable registerClass:[threeTableViewCell class] forCellReuseIdentifier:customCell3];
        
    }
    return _maintable;
}
@end
